<?php

namespace App\Api\User;

use PhalApi\Api;

/**
 * 新版获取菜单
 * @desc 新版获取菜单
 * @author: dogstar 2018-01-09
 */
class Menu extends Api
{
    public function getRules()
    {
        return array(
            'get_menu' => array(
                'username' => array(
                    'name' => 'username',
                    'type' => 'string',
                    'require' => true,
                    'desc' => '用户名',
                ),
            ),
            'get_menu_list' => array(
                'id' => array(
                    'name' => 'id',
                    'type' => 'int',
                    'desc' => '用户ID',
                ),
            ),
            'add_menu' => array(),
            'del_menu' => array(
                'id' => array(
                    'name' => 'id',
                    'type' => 'int',
                    'desc' => '菜单id',
                ),
            ),
            'update_menu' => array(),
        );
    }


    /**
     * 新版获取菜单
     * @desc 获取用户菜单
     * @return array 菜单数据
     */
    public function get_menu()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers:*');
        
        \PhalApi\DI()->response->addResult('success', true);

        $username = $this->username;
        $user = \PhalApi\DI()->notorm->phalapi_user->where('username', $username)->fetchOne();
        if (!$user) {
            \PhalApi\DI()->response->addResult('error', '用户不存在');
            return ['error' => '用户不存在'];
        }
        // return $user;
        $roleIds = json_decode($user['roleIds'], true);
        if (empty($roleIds)) {
            \PhalApi\DI()->response->addResult('error', '用户没有分配角色');
            return ['error' => '用户没有分配角色'];
        }
        $rolePermission = \PhalApi\DI()->notorm->phalapi_portal_admin_role->where('id', $roleIds)->fetchAll();
        $Permission = json_decode($rolePermission[0]['permission'], true);
        // return $rolePermission;

        $data = \PhalApi\DI()->notorm->admin_menu->where('id', $Permission)->fetchAll();
        // 构建菜单树
        $menuTree = [];
        foreach ($data as $item) {
            if ($item['parentId'] == 0) {
                $menuItem = [
                    'path' => $item['path'],
                    'redirect' => $item['redirect'],
                    'meta' => [
                        'icon' => $item['icon'],
                        'title' => $item['title'],
                        'rank' => $item['rank'],
                        'showLink' => $item['showLink'] == 0 ? false : true
                    ],
                    'children' => []
                ];
                // 查找子菜单
                foreach ($data as $child) {
                    if ($child['parentId'] == $item['id']) {
                        $menuItem['children'][] = [
                            'path' => $child['path'],
                            'name' => $child['name'],
                            'component' => $child['component'],
                            'props' => true,
                            'meta' => [
                                'icon' => $child['icon'],
                                'title' => $child['title'],
                                'showLink' => $item['showLink'] == 0 ? false : true
                            ]
                        ];
                    }
                }
                $menuTree[] = $menuItem;
            }
        }
        return $menuTree;
    }

    /**
     * 新版获取菜单详情
     * @desc 获取用户菜单
     * @return array 菜单数据
     */
    public function get_menu_list()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers:*');

        
        \PhalApi\DI()->response->addResult('success', true);
        $data = \PhalApi\DI()->notorm->admin_menu->fetchAll();
        return $data;

    }
    
    /**
     * 新版获取菜单详情
     * @desc 获取用户菜单
     * @return array 菜单数据
     */
    public function add_menu()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers:*');

        $input = json_decode(file_get_contents('php://input'), true);
        if (empty($input['name'])) {
            \PhalApi\DI()->response->addResult('error', 'name字段不能为空');
            return ['error' => 'name字段不能为空'];
        }

        $exists = \PhalApi\DI()->notorm->admin_menu->where('name', $input['name'])->fetchOne();
        if ($exists) {
            \PhalApi\DI()->response->addResult('error', $input['name']."已存在");
            return ['error' => 'name已存在'];
        }

        $result = \PhalApi\DI()->notorm->admin_menu->insert($input);
        \PhalApi\DI()->response->addResult('success', true);
        return $result;
    }

    /**
     * 删除菜单
     * @desc 根据id删除菜单项
     * @return array 操作结果
     */
    public function del_menu()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers:*');

        if (empty($input['id'])) {
            \PhalApi\DI()->response->addResult('error', 'id不能为空');
            return ['error' => 'id不能为空'];
        }
        $id = $input['id'];

        $result = \PhalApi\DI()->notorm->admin_menu->where('id', $id)->delete();
        if ($result === false) {
            \PhalApi\DI()->response->addResult('error', '删除失败');
            return ['error' => '删除失败'];
        }

        \PhalApi\DI()->response->addResult('success', true);
        return ['success' => true];
    }

    /**
     * 更新菜单
     * @desc 根据id更新菜单项
     * @return array 操作结果
     */
    public function update_menu()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers:*');



        $input = json_decode(file_get_contents('php://input'), true);
        if (empty($input)) {
            \PhalApi\DI()->response->addResult('error', '更新数据不能为空');
            return ['error' => '更新数据不能为空'];
        }

        if (empty($input['name'])) {
            \PhalApi\DI()->response->addResult('error', 'name不能为空');
            return ['error' => 'name不能为空'];
        }
        $id = $input['id'];

        $result = \PhalApi\DI()->notorm->admin_menu
            ->where('id', $id)
            ->update($input);

        if ($result === false) {
            \PhalApi\DI()->response->addResult('error', '更新失败');
            return ['error' => '更新失败'];
        }

        \PhalApi\DI()->response->addResult('success', true);
        return ['success' => true];
    }



    
}